package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/">80. 删除有序数组中的重复项 II</a>
 */
public class Solution80 extends BaseSolution {

    public int removeDuplicates(int[] nums) {
        int p = 1, left = 0;
        int lastC = 1;
        while (p < nums.length) {
            boolean growLeft = nums[p] != nums[left] || lastC == 1;
            if (growLeft) {
                lastC = nums[p] == nums[left] ? lastC + 1 : 1;
                left++;
                nums[left] = nums[p];
            }
            p++;
        }
        return left + 1;
    }

    public static void main(String[] args) {
        Solution80 solution = new Solution80();
        int[] target = new int[] {0,0,1,1,1,1,2,3,3};
        System.out.println(solution.removeDuplicates(target));
        printVals(target);
    }
}